home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTCREATE.C < prev    next >
Text File  |  1990-06-20  |  5KB  |  169 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btcreate.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* local headers */
  12. #include "btree_.h"
  13.  
  14. /*man---------------------------------------------------------------------------
  15. NAME
  16.      btcreate - create a btree
  17.  
  18. SYNOPSIS
  19.      int btcreate(filename, m, keysize, fldc, fldv)
  20.      const char *filename;
  21.      int m;
  22.      size_t keysize;
  23.      int fldc;
  24.      const btfield_t fldv[];
  25.  
  26. DESCRIPTION
  27.      The btcreate function creates the btree file named by filename.
  28.      m is the degree of the btree and keysize is the size of the keys.
  29.  
  30.      fldc is the field count.  It specifies the number of fields in
  31.      the keys stored in this btree.  fldv is an array of field
  32.      definition structures.  fldv must have fldc elements.  The field
  33.      definition structure is defined in <btree.h> as type btfield_t.
  34.      It has the following members.
  35.  
  36.           size_t offset;      /* offset of field in key *\/
  37.           size_t len;         /* field length *\/
  38.           int (*cmp)(void *p1, void *p2, size_t n);
  39.                               /* comparison function for field *\/
  40.           int flags;          /* flags *\/
  41.  
  42.      offset and len specify the location and length of the field,
  43.      respectively.  cmp is a pointer to the user written comparison
  44.      function which defines the sort sequence for the field; p1 and p2
  45.      point to the two fields to be compared and n is the field length.
  46.      The return value must be less than, equal to, or greater than
  47.      zero if p1 is less than, equal to, or greater than p2,
  48.      respectively.  btree field flags values are constructed by
  49.      bitwise OR-ing together flags from the following list (one and
  50.      only one of the first two may be used).
  51.  
  52.      BT_FASC        ascending order
  53.      BT_FDSC        descending order
  54.  
  55.      The fields in the field definition list must be in order,
  56.      starting with the first field in the key.
  57.  
  58.      btopen will fail if one or more of the following is true:
  59.  
  60.      [EEXIST]       The named btree file exists.
  61.      [EINVAL]       filename is the NULL pointer.
  62.      [EINVAL]       m is less than 3.
  63.      [EINVAL]       keysize is less than 1.
  64.      [EINVAL]       fldc is less than 1.
  65.      [EINVAL]       fldv is the NULL pointer.
  66.      [EINVAL]       fldv contains an invalid field definition.
  67.      [BTEMFILE]     Too many open btrees.  The maximum is defined as
  68.                     BTOPEN_MAX in btree.h.
  69.  
  70. SEE ALSO
  71.      btopen.
  72.  
  73. DIAGNOSTICS
  74.      Upon successful completion, a value of 0 is returned.  Otherwise,
  75.      a value of -1 is returned, and errno set to indicate the error.
  76.  
  77. ------------------------------------------------------------------------------*/
  78. int btcreate(filename, m, keysize, fldc, fldv)
  79. const char *filename;
  80. int m;
  81. size_t keysize;
  82. int fldc;
  83. const btfield_t fldv[];
  84. {
  85.     btree_t *    btp    = NULL;
  86.     int        terrno    = 0;        /* tmp errno */
  87.  
  88.     /* validate arguments */
  89.     if (filename == NULL || m < 3 || !bt_fvalid(keysize, fldc, fldv)) {
  90.         errno = EINVAL;
  91.         return -1;
  92.     }
  93.  
  94.     /* find free slot in btb table */
  95.     for (btp = btb; btp < (btb + BTOPEN_MAX); ++btp) {
  96.         if (!(btp->flags & BTOPEN)) {
  97.             break;        /* found */
  98.         }
  99.     }
  100.     if (btp >= btb + BTOPEN_MAX) {
  101.         errno = BTEMFILE;    /* no free slots */
  102.         return -1;
  103.     }
  104.  
  105.     /* load btree_t structure */
  106.     btp->bthdr.flh = NIL;
  107.     btp->bthdr.m = m;
  108.     btp->bthdr.keysize = keysize;
  109.     btp->bthdr.flags = 0;
  110.     btp->bthdr.root = NIL;
  111.     btp->bthdr.first = NIL;
  112.     btp->bthdr.last = NIL;
  113.     btp->bthdr.keycnt = 0;
  114.     btp->bthdr.height = 0;
  115.     btp->bp = NULL;
  116.     btp->flags = BTREAD | BTWRITE;
  117.     btp->fldc = fldc;            /* fields */
  118.     btp->fldv = NULL;
  119.     btp->cbtpos.node = NIL;            /* cursor */
  120.     btp->cbtpos.key = 0;
  121.     btp->cbtnp = NULL;
  122.     btp->sp = NULL;
  123.     if (bt_alloc(btp) == -1) {
  124.         BTEPRINT;
  125.         terrno = errno;
  126.         memset(btp, 0, sizeof(*btb));
  127.         btp->flags = 0;
  128.         errno = terrno;
  129.         return -1;
  130.     }
  131.     memcpy(btp->fldv, fldv, btp->fldc * sizeof(*btp->fldv));
  132.  
  133.     /* create file */
  134.     btp->bp = bopen(filename, "c", sizeof(bthdr_t), (size_t)1, (size_t)0);
  135.     if (btp->bp == NULL) {
  136.         if (errno != EEXIST) BTEPRINT;
  137.         terrno = errno;
  138.         bt_free(btp);
  139.         memset(btp, 0, sizeof(*btb));
  140.         btp->flags = 0;
  141.         errno = terrno;
  142.         return -1;
  143.     }
  144.  
  145.     /* write header to file */
  146.     if (bputh(btp->bp, &btp->bthdr) == -1) {    /* header */
  147.         BTEPRINT;
  148.         terrno = errno;
  149.         bclose(btp->bp);
  150.         bt_free(btp);
  151.         memset(btp, 0, sizeof(*btb));
  152.         btp->flags = 0;
  153.         errno = terrno;
  154.         return -1;
  155.     }
  156.  
  157.     /* close btp */
  158.     if (btclose(btp) == -1) {
  159.         BTEPRINT;
  160.         terrno = errno;
  161.         bt_free(btp);
  162.         errno = terrno;
  163.         return -1;
  164.     }
  165.  
  166.     errno = 0;
  167.     return 0;
  168. }
  169.